package controllers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import controllers.Tutorials.*;

import models.*;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import play.data.validation.Email;
import play.data.validation.Equals;
import play.data.validation.Required;
import play.db.jpa.Blob;
import play.db.jpa.GenericModel;
import play.db.jpa.GenericModel.JPAQuery;
import play.db.jpa.Model;
import play.libs.Codec;
import play.libs.Mail;
import play.mvc.Controller;

public class Application extends Controller {

	private static Object add;

	/**
	 * This method renders the html of the signup
	 * 
	 * @author mohammed
	 */
	public static void signUp() {
		render();
	}

	// public static void openTutorial(){
	// render();
	// viewPublicComments();
	// viewPrivateComments();
	// }

	/**
	 * This method views all the profile content
	 * 
	 * @author mohammed
	 */
	public static void viewProfile(long id) {
		id = Long.parseLong(session.get("RegisteredUserId"));
		render(id);
	}

	// Rana Amr
	// public static void Exercise()
	// {
	// render();
	// }

	/**
	 * This method add users to the database and makes the registeration
	 * validation
	 * 
	 * @author mohammed
	 */

	public static void addUser(
			@Required(message = "Please Enter Your nick Name") String name,
			@Required(message = "Please Enter Your E-mail") @Email(message = "Please Enter a Valid e-mail") String email,
			@Required(message = "Repeating the same email is required") @Email(message = "Please Enter a Valid e-mail") @Equals("email") String remail,
			@Required(message = "Password is required") String pass,
			@Required(message = "repeating the same Password is required") @Equals("pass") String rpass)
			throws EmailException {

		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			signUp();

		}
		if (RegisteredUser.isExist(email)) {
			validation.addError("email", "This e-mail is already registered");
			params.flash();
			validation.keep();
			signUp();
		}

		validate(name, email, pass);
	}

	/**
	 * This method checks if the user logged in to render him to home after he
	 * logs in
	 * 
	 * @author mohammed
	 */
	public static void Home() {
		if (session.get("RegisteredUserId") != null) {
			long uid = Long.parseLong(session.get("RegisteredUserId"));
			RegisteredUser u = RegisteredUser.findById(uid);
			render(u);
		} else {
			render();
		}

	}

	/**
	 * This method checks if the user is valid to log or not and if yes connects
	 * him if not sends him a validation
	 * 
	 * @author mohammed
	 */
	public static void logIn(
			@Required(message = "Please Enter Your E-mail !!") @Email(message = "Please Enter a Valid e-mail !!") String email,
			@Required(message = "Password is required !!") String password) {
		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			updates();
		}
		if (RegisteredUser.isValidLogin(email, password)) {
			RegisteredUser u = RegisteredUser.connect(email, password);
			session.put("RegisteredUserId", u.id);
			// Logging ....
			if (!u.isAdmin) {
				LogRecord.createLog("Logged In", u);
			}

			updates();
		} else {

			validation.addError("email", "Invalid Login");
			validation.keep();
			updates();
		}
	}

	/**
	 * This method removes the user id from the session to make him logout
	 * 
	 * @author mohammed
	 */
	public static void logOut() {
		session.remove("RegisteredUserId");
		signUp();
	}

	// aya marawan
	/**
	 * start @author ayatullah marawan In this user story, the user can create
	 * community list where when The user clicks on the create community list
	 * button, then he will be transferred to the create page
	 * "createCommListPage" and he/she types in the name of the community list
	 * and then when press enter the communityList will be created using method
	 * createCommunityList(@Required String text) and the user will be
	 * transfered to the page of this community List end @author Ayatullah
	 * MArawan
	 * 
	 * @param text
	 *            CommunityList name
	 */

	// aya marawan
	/**
	 * start @author ayatullah marawan In this user story, the user can create
	 * community list where when The user clicks on the create community list
	 * button, then he will be transferred to the create page
	 * "createCommListPage" and he/she types in the name of the community list
	 * and then when press enter the communityList will be created using method
	 * createCommunityList(@Required String text) and the user will be
	 * transfered to the page of this community List end @author Ayatullah
	 * MArawan
	 * 
	 * @param text
	 *            CommunityList name
	 */

	// Start -----> Omnia
	/**
	 * This method retrieve the requests-To-Contribute submitted to the
	 * logged-in user in a list and renders this list
	 * 
	 * @param RegisteredUserID
	 *            ID of the logged-in user Story [C3][S9]
	 * @author Omnia
	 */
	public static void contributeRequests() {
		if (session.get("RegisteredUserId") != null) {
			long replierId = Long.parseLong(session.get("RegisteredUserId"));
			List<Request> reqList = Request.find(
					"replierID =? and isConfirmed =? and isRejected =? ",
					replierId, false, false).fetch();
			render(reqList);
		} else {
			renderText("you are not logged-in please log in");
		}
	}

	/**
	 * This Method to confirm a requestToContribute to the tutorial, this is
	 * called by the creator(owner) of the tutorial ONLY!! it sets isConfirmed
	 * attribute of the current request to true and adds the requester to the
	 * list of contributors of the tutorial of the request
	 * 
	 * @param okay
	 *            Request-id Story [C3][S9]
	 * @author Omnia
	 */
	public static void confirmRequestToContribute(long okay) {
		if (session.get("RegisteredUserId") != null) {
			Request myReq = Request.findById(okay);
			myReq.setConfirmed();
			myReq.save();
			RegisteredUser user = RegisteredUser.findById(myReq.requesterID);
			Tutorial tut = Tutorial.findById(myReq.entityID);
			if (!user.contributedTutorials.contains(tut)) {
				user.updatecontributedTutorialsList(tut);
				user.save();
				new Notification(myReq.replierID, myReq.requesterID,
						myReq.entityID, 7).save();
			}
			long usertwo = Long.parseLong(session.get("RegisteredUserId"));
			viewProfile(usertwo);
		} else {
			renderText("you are not logged-in please log in");
		}
	}

	/**
	 * This Method to reject a requestToContribute to the tutorial, this is
	 * called by the creator(owner) of the tutorial ONLY!! it sets isRejected
	 * attribute of the current request to true
	 * 
	 * @param notokay
	 *            Request-id
	 * @author Omnia
	 */
	public static void rejectRequestToContribute(long notokay) {
		if (session.get("RegisteredUserId") != null) {

			Request myReq = Request.findById(notokay);
			myReq.setRejected();
			myReq.save();
			;
			Tutorial tutorial = Tutorial.findById(myReq.entityID);
			long user = Long.parseLong(session.get("RegisteredUserId"));
			viewProfile(user);
		} else {
			renderText("you are not logged-in please log in");
		}
	}

	// End -----> Omnia

	// Ragab

	/**
	 * 
	 * this method let the user to download the media file by clicking on
	 * download button which appears above the media files
	 * 
	 * @author Mohamed Ahmed Ragab
	 * 
	 * @param long id
	 * 
	 */
	public static void photoDownload(long id) {
		final Media media = Media.findById(id);
		response.setContentTypeIfNotSet(media.photo.type());
		java.io.InputStream binaryData = media.photo.get();
		renderBinary(binaryData, media.name);
	}

	/**
	 * 
	 * this method is responsible to return the media files and shows it to the
	 * user in the relation html after uploading
	 * 
	 * @author Mohamed Ahmed Ragab
	 * 
	 * @param long id
	 * 
	 */
	public static void userPhoto(long id) {
		final Media media = Media.findById(id);

		System.out.println(media.type);
		System.out.println(media.photo.type());

		notFoundIfNull(media);
		String type = media.photo.type();
		String temp = "";
		response.setContentTypeIfNotSet(media.photo.type());
		renderBinary(media.photo.get());
	}

	// end of Ragab

	public static void usersList() {
		List<RegisteredUser> users = RegisteredUser.findAll();
		render(users);
	}

	/**
	 * this method lets the admin set an user to a housekeeper from the user
	 * list which is only available for the admin
	 * 
	 * @author Yasmin Gamal
	 * @param UserID
	 */
	public static void setUserToHousekeeper(Long UserID) {
		long id = Long.parseLong(session.get("RegisteredUserId"));
		RegisteredUser userChosen = RegisteredUser.findById(UserID);
		if (id != UserID) {
			if (userChosen.isHousekeeper == false) {
				userChosen.setHousekeeper(true);
				userChosen.save();
				new Notification(id, UserID, 0, 25).save();
				renderText("the user is now a housekeeper");
			} else {
				renderText("The user is already a Housekeeper");
			}
		} else {
			renderText("You can not set yourself as a housekeeper");
		}
	}

	/*
	 * This method renders an a list of private comments that allows only
	 * registered users that are contributors to view all the private comments
	 * posted on a certain tutorial.
	 * 
	 * @author Rana A. Zakaria
	 * 
	 * 
	 * public static void viewPrivateComments() { session.put("userId", 1);
	 * session.put("tutorialId", 1); long uid =
	 * Long.parseLong(session.get("userId")); models.RegisteredUser u =
	 * models.RegisteredUser.findById(uid);
	 * 
	 * long tid = Long.parseLong(session.get("tutorialId")); models.Tutorial t =
	 * models.Tutorial.findById(tid);
	 * 
	 * if (t.contributorsList.contains(uid)) { List <Comment> y = t.pcomments;
	 * render(y); } }
	 */

	/*
	 * This method allows the user to add a private comment on a certain
	 * tutorial where it renders a list of private comments in addition to the
	 * comment that the user has added. However only the contributors are
	 * allowed to see the private comments.
	 * 
	 * @author Rana A. Zakaria
	 */

	/*
	 * public static void createPrivateComment(String comment, Tutorial tut,
	 * RegisteredUser commentor, boolean isPrivate) { session.put("userId", 1);
	 * session.put("tutorialId", 1); long uid =
	 * Long.parseLong(session.get("userId")); models.RegisteredUser u =
	 * models.RegisteredUser.findById(uid);
	 * 
	 * long tid = Long.parseLong(session.get("tutorialId")); models.Tutorial t =
	 * models.Tutorial.findById(tid); List <Comment> h = t.pcomments;
	 * 
	 * Comment g = new Comment(comment, tut, commentor, isPrivate).save(); if
	 * (comment == null) { System.out.println("Input is invalid"); } else {
	 * h.add(g); if (t.contributorsList.contains(uid)) { renderJSON(h); } else {
	 * System.out.print("Your comment has been posted"); }
	 * 
	 * }
	 * 
	 * // End of two methods @author Rana A. Zakaria
	 */

	/**
	 * This method renders all published tutorial titles and topics on the web
	 * site
	 * 
	 * @return render a text "please login to view this page" when there is no
	 *         user logged in
	 * @author Yasmine A.
	 **/
	public static void updates() {
		if (session.get("RegisteredUserId") == null) {
			renderText("please login to view this page");
		} else {

			List<Topic> topics = Topic.find("order by topicName asc").fetch();

			List<Tutorial> tutorials = Tutorial.find("status=?", "publish")
					.fetch();

			if (session.get("RegisteredUserId") != null) {
				long uid = Long.parseLong(session.get("RegisteredUserId"));
				RegisteredUser user = RegisteredUser.findById(uid);
				render(topics, tutorials, user);
			}

			else {
				render(topics, tutorials);
			}
		}

	}

	/**
	 * This method renders an array of tutorials of a specific topic chosen by
	 * the user
	 * 
	 * @return render a text "please login to view this page" when there is no
	 *         user logged in
	 * @param id
	 *            topic id to render a list of tuts with that topic id
	 * @author Yasmine A.
	 **/
	public static void topicUpdates(Long id) {
		if (session.get("RegisteredUserId") == null) {
			renderText("please login to view this page");
		} else {

			List<Topic> topics = Topic.find("order by topicName asc").fetch();

			models.Topic topic = models.Topic.findById(id);
			String topicName = topic.topicName;

			List<Tutorial> topicTuts = Tutorial.find("Topic=?", topicName)
					.fetch();

			render(topicTuts, topics, topicName);
		}

	}

	/**
	 * this method renders a list of followed topics and their tutorials
	 * 
	 * @return render a text "please login to view this page" when there is no
	 *         user logged in
	 * @author Yasmine A.
	 */
	public static void followedUpdatesByTopic() {

		if (session.get("RegisteredUserId") == null) {
			renderText("please login to view this page");
		} else {
			long userId = Long.parseLong(session.get("RegisteredUserId"));
			models.RegisteredUser user = models.RegisteredUser.findById(userId);
			List<Tutorial> updatesToSend = new ArrayList<Tutorial>();
			{
				if (user.followedTops.size() > 0) {
					for (int i = 0; i < user.followedTops.size(); i++) {
						Topic topicName = user.followedTops.get(i);

						List<Tutorial> topicTuts = Tutorial.find("topicT=?",
								topicName).fetch();

						for (int x = 0; x < topicTuts.size(); x++) {
							updatesToSend.add(topicTuts.get(x));
						}
					}
				}
			}

			List<Topic> followedTopics = new ArrayList<Topic>();
			for (int x = 0; x < user.followedTops.size(); x++) {
				followedTopics.add(user.followedTops.get(x));
			}

			render(updatesToSend, followedTopics);
		}
	}

	/**
	 * this method shows tutorial titles of followed tutorials
	 * 
	 * @return render a text "please login to view this page" when there is no
	 *         user logged in
	 * @author Yasmine A.
	 */
	public static void followedUpdatesByTutorial() {

		if (session.get("RegisteredUserId") == null) {
			renderText("please login to view this page");
		} else {
			long userId = Long.parseLong(session.get("RegisteredUserId"));
			models.RegisteredUser user = models.RegisteredUser.findById(userId);
			List<Tutorial> updatesToSend = new ArrayList<Tutorial>();

			if (user.followedTuts.size() > 0) {
				for (int i = 0; i < user.followedTuts.size(); i++) {
					String tutName = user.followedTuts.get(i).Title;
					List<Tutorial> tuts = Tutorial.find("Title=?", tutName)
							.fetch();

					for (int f = 0; f < tuts.size(); f++) {

						updatesToSend.add(tuts.get(f));
					}

				}
			}

			render(updatesToSend);
		}

	}

	/**
	 * this method renders a list of followed users and a list of tutorial title
	 * that these followed user's have created
	 * 
	 * @return render a text "please login to view this page" when there is no
	 *         user logged in
	 * @author Yasmine A.
	 */
	public static void followedUpdatesByUser() {

		if (session.get("RegisteredUserId") == null) {
			renderText("please login to view this page");
		} else {
			long userId = Long.parseLong(session.get("RegisteredUserId"));
			models.RegisteredUser user = models.RegisteredUser.findById(userId);
			List<Tutorial> updatesToSend = new ArrayList<Tutorial>();
			if (user.followedUsrs.size() > 0) {
				for (int i = 0; i < user.followedUsrs.size(); i++) {
					RegisteredUser creatorName = user.followedUsrs.get(i);
					List<Tutorial> userTuts = Tutorial.find("creator=?",
							creatorName).fetch();
					for (int f = 0; f < userTuts.size(); f++) {
						updatesToSend.add(userTuts.get(f));
					}

				}
			}
			List<RegisteredUser> followedUsers = new ArrayList<RegisteredUser>();
			for (int x = 0; x < user.followedUsrs.size(); x++) {
				followedUsers.add(user.followedUsrs.get(x));
			}
			render(updatesToSend, followedUsers);
		}
	}

	/*
	 * This method views all the tutorials
	 * 
	 * @author mohammed
	 */
	public static void TutorialDumView() {
		long uid = Long.parseLong(session.get("RegisteredUserId"));
		RegisteredUser u = RegisteredUser.findById(uid);

		List<Object> tuts = Tutorial.find("creator = ?", u).fetch();
		render(tuts);

	}

	/**
	 * This method makes the user after registeration gets a mail with his
	 * information
	 * 
	 * @author mohammed
	 * @param name
	 *            , @param email, @param code,@param password
	 */
	public static void sendEmail(String name, String userEmail, String code,
			String pass) throws EmailException {
		SimpleEmail email = new SimpleEmail();
		email.setFrom("sharesoft.ibm@gmail.com");

		email.addTo(userEmail);

		email.setSubject("Sharesoft-12 website: Email validation");

		email.setMsg("Welcome user to Share-soft2012 website\n "
				+ "Enter these nubmers in the validation textbox without any spaces\n"
				+ code);

		Mail.send(email);

		List<String> s = new ArrayList<String>();
		s.add(name);
		s.add(userEmail);
		s.add(pass);

		validation(s);

	}

	/**
	 * This method makes the code that will be sent to the user
	 * 
	 * @author mohammed returns the code
	 */
	public static String getValidationNum() {

		int[] random = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		String code = "";
		for (int i = 0; i < 10; i++) {
			double randomnumber = Math.random();

			if (randomnumber >= 0 && randomnumber < .1)
				code = code + (random[0] + "");
			if (randomnumber >= .1 && randomnumber < .2)
				code = code + (random[1] + "");
			if (randomnumber >= .2 && randomnumber < .3)
				code = code + (random[2] + "");
			if (randomnumber >= .3 && randomnumber < .4)
				code = code + (random[3] + "");
			if (randomnumber >= .4 && randomnumber < .5)
				code = code + (random[4] + "");
			if (randomnumber >= .5 && randomnumber < .6)
				code = code + (random[5] + "");
			if (randomnumber >= .6 && randomnumber < .7)
				code = code + (random[6] + "");
			if (randomnumber >= .7 && randomnumber < .8)
				code = code + (random[7] + "");
			if (randomnumber >= .8 && randomnumber < .9)
				code = code + (random[8] + "");
		}
		return code;
	}

	/**
	 * This method makes makes the validation of sending the mail
	 * 
	 * @author mohammed
	 * @param name
	 *            , @param email,@param password
	 */

	public static void validate(String name, String mail, String pass)
			throws EmailException {
		String code = getValidationNum();
		session.put("code", code);
		sendEmail(name, mail, code, pass);
	}

	/**
	 * This method makes renders the validation
	 * 
	 * @author mohammed
	 * @param s
	 *            the list of the list of the user name, email, and password
	 */
	public static void validation(List<String> s) {
		render(s);
	}

	/**
	 * This method finds the user to add him
	 * 
	 * @author mohammed
	 * @param name
	 *            , @param email,@param password
	 */

	public static void finAdd(String name, String mail, String pass) {
		RegisteredUser u = new RegisteredUser(name, mail, pass, false);
		u.save();
		// Logging ....
		LogRecord.createLog("Signed Up", u);
		System.out.println(RegisteredUser.findAll().size());
		u = RegisteredUser.connect(u.mail, u.password);
		session.put("RegisteredUserId", u.id);
		updates();
	}

	/**
	 * This method takes the id of the user from the session and pass it to the
	 * method isAdmin in RegisteredUser model to check if true or false returns
	 * true or false
	 * 
	 * @author mohammed mattr
	 */

	public static boolean isAdminhelper() {

		if (session.get("RegisteredUserId") != null) {
			long userId = Long.parseLong(session.get("RegisteredUserId"));
			return models.RegisteredUser.isAdmin(userId);
		}
		return false;

	}

	/**
	 * This method renders the html of the LogView and makes the list ordered by
	 * id
	 * 
	 * @author mohammed mattr
	 */

	public static void LogView() {

		List<LogRecord> logs = LogRecord.findAll();
		LogRecord temp;

		for (int i = 0; i < logs.size(); i++) {

			for (int k = i; k < logs.size(); k++) {
				if (logs.get(k).id > logs.get(i).id) {
					temp = logs.get(i);
					logs.set(i, logs.get(k));
					logs.set(k, temp);
				}
			}
		}

		render(logs);

	}

	/**
	 * This method renders others profile page content
	 * 
	 * @author Heidi ElToukhy
	 * @param long viewOthers
	 */
	public static void othersProfile(long viewOthers) {

		long x = viewOthers;

		render(x);
	}

	/**
	 * This method checks whether the profile to be opened is that of the
	 * registered user's profile or others profile and renders the right one and
	 * yet it first checks whether the registeredUser who is about to open the
	 * profile is in the private list to block him from opening the profile or
	 * in the public list to retrieve the profile normally.
	 * 
	 * @author Heidi ElToukhy
	 * @param long viewTheProfile
	 */
	public static void seeOthersProfile(long viewTheProfile) {

		long x = Long.parseLong(session.get("RegisteredUserId"));
		RegisteredUser RegUser = RegisteredUser.findById(x);

		if (x == viewTheProfile) {

			viewProfile(x);
		} else {

			long viewOthers = viewTheProfile;
			RegisteredUser user = RegisteredUser.findById(viewOthers);
			if (user.privateList.contains(RegUser)) {
				renderText("you can not enter profile its private");
			}
			if (user.publicList.contains(RegUser)) {
				othersProfile(viewOthers);
			} else {
				if (user.isPrivate == true) {
					renderText("you can not enter profile its private to all and ur not in yhe public list :D");
				}
			}

			othersProfile(viewOthers);

		}
	}

	/**
	 * This method opens the search page for user to search for other membrs
	 * 
	 * @author Ayatullah Marawan
	 */

	public static void searchPage() {
		render();
	}

	/**
	 * In this method the user can search for all other users, where he clicks
	 * on button search for all members then all users show to the user.
	 * 
	 * @author Ayatullah Marawan
	 */
	public static void allMembers() {

		List allUsers = RegisteredUser.find("order by id desc").fetch();
		render(allUsers);

	}

	/**
	 * This method user can search for other users by email where he enters the
	 * e-mail of certain user in the search bar then all users having the same
	 * name will show to the user.
	 * 
	 * @author Ayatullah Marawan
	 * @param String
	 *            mailText
	 */

	public static void searchByMailToOpenProfile(@Required String mailText) {

		if (validation.hasErrors()) {
			flash.error("Oops, please enter e-mail!");
			searchPage();
		}

		String mail = mailText;
		List usersByMail = RegisteredUser.find("mail=?", mail).fetch();
		render(usersByMail);
	}

	/**
	 * This method user can search for other users by name where he enters the
	 * name of certain user in the search bar then all users having the same
	 * name will show to the user.
	 * 
	 * @author Ayatullah Marawan
	 * @param String
	 *            nameText
	 */
	public static void searchByNameToOpenProfile(@Required String nameText) {
		if (validation.hasErrors()) {
			flash.error("Oops, please enter name!");
			searchPage();
		}

		String Name = nameText;
		List usersByName = RegisteredUser.find("byNameLike", "%" + Name + "%")
				.fetch();
		render(usersByName);
	}

	/**
	 * This method retrieve to the registered user all other users,
	 * 
	 * @author Ayatullah Marawan
	 */
	public static void searchForAllMembers() {

		List allUsers = RegisteredUser.find("order by id desc").fetch();
		render(allUsers);

	}

	/**
	 * This method takes a boolean x and checks that if it is true, then remove
	 * the user with the same id of the taken button id REMOVE from the
	 * publicList or else if it is false then remove from the privateList
	 * 
	 * @author Heidi ElToukhy
	 * @param long remove boolean x
	 */

	public static void removeFromList(long remove, boolean x) {

		long uid = Long.parseLong(session.get("RegisteredUserId"));

		RegisteredUser registeredUser = RegisteredUser.findById(uid);

		RegisteredUser removedUser = RegisteredUser.findById(remove);

		if (x == true) {
			registeredUser.publicList.remove(removedUser);
			registeredUser.save();
		} else {
			registeredUser.privateList.remove(removedUser);
			registeredUser.save();
		}
	}

	/**
	 * This method able the Registered user to view the list of members that the
	 * Registered User put in his private List for privacy settings
	 * 
	 * @author Ayatullah Marawan
	 */
	public static void showPrivateList() {
		long uid = Long.parseLong(session.get("RegisteredUserId"));
		RegisteredUser registeredUser = RegisteredUser.findById(uid);

		List<RegisteredUser> privateList = registeredUser.privateList;
		render(privateList);

	}

	/**
	 * This method takes list of ids of users that registered user had chosen
	 * and put it in his private list.
	 * 
	 * @author Ayatullah Marawan
	 * @param long[] ids
	 */
	public static void addToPrivateList(long[] ids) {
		if (ids == null) {
			renderText("No users were choosen to add!");
		}
		for (Long id : ids) {

			RegisteredUser privateUser = RegisteredUser.findById(id);
			RegisteredUser registeredUser = RegisteredUser.findById(Long
					.parseLong(session.get("RegisteredUserId")));

			if (registeredUser.publicList.contains(privateUser)) {
				registeredUser.publicList.remove(privateUser);
				registeredUser.privateList.add(privateUser);
				registeredUser.save();
			}
			registeredUser.privateList.add(privateUser);
			registeredUser.save();

		}

		renderText("u have succ added the chosen members to private list");

	}

	/**
	 * This method renders a list of the members that the registeredUser chose
	 * to put in the publicList for privacy settings
	 * 
	 * @author Heidi ElToukhy
	 * @param no
	 *            parameters
	 */

	public static void showPublicList() {
		long id = Long.parseLong(session.get("RegisteredUserId"));

		RegisteredUser registeredUser = RegisteredUser.findById(id);

		List<RegisteredUser> publicList1 = registeredUser.publicList;
		render(publicList1);

	}

	/**
	 * This method takes a list of users ids that registeredUser chose and put
	 * it in his publicList.
	 * 
	 * @author Heidi ElToukhy
	 * @param long[] ids
	 */

	public static void addToPublicList(long[] ids) {
		if (ids == null) {

			renderText("No users were choosen to add!");
		}
		for (Long id : ids) {

			RegisteredUser publicUser = RegisteredUser.findById(id);
			RegisteredUser registeredUser = RegisteredUser.findById(Long
					.parseLong(session.get("RegisteredUserId")));

			if (registeredUser.privateList.contains(publicUser)) {
				registeredUser.privateList.remove(publicUser);
				registeredUser.publicList.add(publicUser);
				registeredUser.save();
			}

			registeredUser.publicList.add(publicUser);
			registeredUser.save();

		}
		renderText("u have succ added the chosen members to public list");

	}

	/**
	 * This method takes the notificationId and renders a list of notifications
	 * I have.
	 * 
	 * @return a list of notifications
	 * 
	 * @author mayar
	 */

	public static void viewNotifications() {

		if (session.get("RegisteredUserId") != null) {
			{
				long userId = Long.parseLong(session.get("RegisteredUserId"));
				List<Notification> notificationList = Notification.find(
						"receiverId=? and seen=?", userId, false).fetch();
				render(notificationList);
			}
		}

		renderText("Please login to see your notifications.");
	}

	/**
	 * This method sets the status of notifications to seen
	 * 
	 * @param notificationId
	 *            which is the id of the notification I received.
	 * 
	 * @author mayar
	 */

	public static void setSeen(long notificationId) {
		// renderText("Entered The Method");
		Notification notifications = Notification.findById(notificationId);
		notifications.setSeen();
	}

}
